package com.litong.litongjiaoyu.controller;

import com.litong.litongjiaoyu.service.SysChatrecordService;
import com.litong.litongjiaoyu.service.SysWhochatwithwhomService;
import com.litong.litongjiaoyu.utils.JSONResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * (SysWhochatwithwhom)表控制层
 *
 * @author jingzepei
 * @since 2020-06-02 09:36:44
 */
@RestController
@RequestMapping("Whochatwithwhom")
public class SysWhochatwithwhomController {
    /**
     * 日志
     */
    private Logger LOOGER = LoggerFactory.getLogger(SysWhochatwithwhomController.class);


    /**
     * 谁与谁聊天记录服务对象
     */
    @Resource
    private SysWhochatwithwhomService sysWhochatwithwhomService;

    /**
     * 详细聊天记录服务对象
     */
    @Resource
    private SysChatrecordService sysChatrecordService;

    /**
     * 获取所有聊天记录
     *
     * @return 所有数据
     */
    @GetMapping("get")
    public JSONResult selectAll() {
        return JSONResult.ok(this.sysWhochatwithwhomService.queryAll());
    }


    /**
     * 删除指定聊天记录
     * @param id
     * @return
     */
    @GetMapping("/delete/{id}")
    public JSONResult delete(@PathVariable("id") String id) {
        try {
            if(this.sysWhochatwithwhomService.deleteById(id)) {
                if (this.sysChatrecordService.deleteById(id)) {
                    return JSONResult.ok();
                } else {
                    throw new RuntimeException(String.format(
                            "sys_chatrecord数据表中ID为：%s的数据删除失败，请手动删除",
                            id
                    ));
                }
            } else {

                throw new RuntimeException(String.format(
                        "sys_whochatwithwhom数据表中ID为：%s的数据删除失败，请手动删除",
                        id
                ));
            }
        } catch (Exception e) {
            LOOGER.error(e.getMessage());
            return JSONResult.errorException(e.getMessage());
        }
    }

}